home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 20 code / Scripting the Finder / Finder Tricks / Main.cp < prev    next >
Encoding:
Text File  |  1994-10-04  |  4.9 KB  |  204 lines  |  [TEXT/MMCC]

  1. /*================================================================================
  2.     Zawphing
  3.     
  4.     ©1994 Greg Anderson
  5.     greggor@apple.com
  6.     
  7.     A program that sends events to the Finder
  8.         
  9. ================================================================================*/
  10. #include <GestaltEqu.h>
  11. #include <Palettes.h>
  12.  
  13. #include "Main.h"
  14. #include "TricksDialog.h"
  15.  
  16. #include "MenuHandler.h"
  17. #include "EventHandler.h"
  18.  
  19. #include "MacUtilities.h"
  20. #include "DialogUtilities.h"
  21. #include "AppleEventUtilities.h"
  22. #include "Exceptions.h"
  23.  
  24. //
  25. // Prototypes for private functions:
  26. //
  27. void                    InitAll(void);
  28. pascal OSErr            QuitApplicationEvent(TAEvent& ae, TAEvent& reply, long refCon);
  29.  
  30. //
  31. // Globals defined in this file:
  32. //
  33. Rect                    gUniverseRect;
  34. RgnHandle                gUniverseRgn = nil;
  35. RgnHandle                gScratchRgn = nil;
  36. GrafPtr                    gWindowMgrPort = nil;
  37.  
  38. SysEnvRec                gThisMacintosh;
  39. Boolean                    gHasAppleEvents;
  40. Boolean                    gApplicationShouldQuit = false;
  41.  
  42. #if USESROUTINEDESCRIPTORS
  43.     static RoutineDescriptor gQuitApplicationHandlerRD    = BUILD_ROUTINE_DESCRIPTOR(uppAEEventHandlerProcInfo, QuitApplicationEvent);
  44. #endif
  45.  
  46. //----------------------------------------------------------------------------------------
  47. // main: 
  48. //----------------------------------------------------------------------------------------
  49. void main()
  50. {
  51.     RgnHandle        mouseRegion;
  52.     AppFile            theFile;
  53.     DialogPtr        splash;
  54.     short            preLoadFiles;
  55.     short            message;
  56.     short            i;
  57.     OSErr            err = noErr;
  58.     
  59.     //
  60.     // Initialize all of the ToolBox managers, change the cursor
  61.     // shape to a watch and display the splash screen.
  62.     //
  63.     InitAll();
  64.     ChangeCursor( watchCursor );
  65.     SetupMenuBar(1001, 1002);
  66.     
  67.     /*
  68.     // Schlep together our command table
  69.     //
  70.     // (command ID, menu ID, item #)
  71.     */
  72.     AddItemIDtoTable( 1, 2, 3 );
  73.     AddItemIDtoTable( 2, 2, 1 );
  74.     
  75.     //
  76.     // Every high-level-event-aware application needs a
  77.     // quit-application handler; otherwise, it won't quit
  78.     // when the machine is shut down (for example)
  79.     //
  80. #if USESROUTINEDESCRIPTORS
  81.     AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, &gQuitApplicationHandlerRD, 0, false);
  82. #else
  83.     AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, (AEEventHandlerProcPtr) &QuitApplicationEvent, 0, false);
  84. #endif
  85.         
  86.     //
  87.     // Set the cursor back to an arrow
  88.     //
  89.     InitCursor();
  90.     
  91.     //
  92.     // Set up the initial mouse region
  93.     //
  94.     mouseRegion = NewRgn();
  95.     
  96.     //
  97.     // Set up a failure handler for failures that are not
  98.     // trapped elsewhere
  99.     //
  100.     Try
  101.     {
  102.         //
  103.         // When the program begins, open our dialog box.
  104.         // If the dialog is ever closed, we will quit (DA model)
  105.         //
  106.         OpenTricksDialog();
  107.         
  108.         //
  109.         // Live here until the 'gApplicationShouldQuit' flag
  110.         // becomes true
  111.         //
  112.         while(gApplicationShouldQuit == false)
  113.             HandleEvents(mouseRegion);
  114.     }
  115.     Catch(err)
  116.     {
  117.         //
  118.         // We don't expect to ever get here...
  119.         //
  120.     }
  121. } // main 
  122.  
  123. //----------------------------------------------------------------------------------------
  124. // InitAll: 
  125. // 
  126. // Initialize various Macintosh managers
  127. //----------------------------------------------------------------------------------------
  128. void InitAll()
  129. {
  130.     OSErr    theErr;
  131.     long    heapSpace;
  132.     Ptr        appLimit;
  133.     THz        appBase;
  134.     long    gestaltResult;
  135.     short    callsToMoreMasters = 10;
  136.     
  137.     appBase = ApplicZone();
  138.     appLimit = GetApplLimit();
  139.     heapSpace = FreeMem();
  140.     MaxApplZone();
  141.     appLimit = GetApplLimit();
  142.     heapSpace = FreeMem();
  143.     while( callsToMoreMasters-- )
  144.         MoreMasters();
  145.     
  146.     InitGraf(&qd.thePort);
  147.     InitFonts();
  148.     FlushEvents(everyEvent, 0);
  149.     InitWindows();
  150.     InitMenus();
  151.     TEInit();
  152.     InitDialogs(0L);
  153.     
  154.     //
  155.     // Get the SysEnvirons record
  156.     //
  157.     SysEnvirons( 1, &gThisMacintosh );
  158.  
  159.     //
  160.     // Check to see if AppleEvents are available
  161.     //
  162.     theErr = Gestalt( gestaltAppleEventsAttr, &gestaltResult );
  163.     gHasAppleEvents = ( (theErr == noErr) && ((gestaltResult & (1L << gestaltAppleEventsPresent)) != 0) );
  164.     
  165.     //
  166.     // Set a global rectangle to hold the extent of QuickDraw workspace
  167.     // (Note:  QuickDraw coordinates range from +-32767, but there
  168.     //  are bugs in QuickDraw that make it inadvisable to go beyond
  169.     //  +-16000 or so.)
  170.     //
  171.     SetRect(&gUniverseRect,-16000,-16000,16000,16000);
  172.     gUniverseRgn = NewRgn();
  173.     RectRgn(gUniverseRgn, &gUniverseRect);
  174.     gScratchRgn = NewRgn();
  175.     GetPort(&gWindowMgrPort);
  176. } // InitAll 
  177.  
  178. //----------------------------------------------------------------------------------------
  179. // ExitProgram: 
  180. //----------------------------------------------------------------------------------------
  181. OSErr ExitProgram(CWindowPtr window, short item)
  182. {
  183.     //
  184.     // A "real" program would try to close all of its windows
  185.     // first (give the user a chance to cancel)
  186.     //
  187.     gApplicationShouldQuit = true;
  188.     
  189.     return noErr;
  190. } // ExitProgram 
  191.  
  192. //----------------------------------------------------------------------------------------
  193. // QuitApplicationEvent: 
  194. //----------------------------------------------------------------------------------------
  195. pascal OSErr QuitApplicationEvent(TAEvent& ae, TAEvent& reply, long refCon)
  196. {
  197.     //
  198.     // You should never call ExitToShell from an AppleEvent handler
  199.     //
  200.     gApplicationShouldQuit = true;
  201.     
  202.     return noErr;
  203. }
  204.